//
//  MPOtherHomeVC.m
//  MPOtherHomeDemo
//
//  Created by YC on 2018/9/5.
//  Copyright © 2018年 yc. All rights reserved.
//

#import "MPOtherHomeVC.h"
#import "MPOtherHomeHeaderView.h"
#import "MPOtherHomeCommentCell.h"
#import "MPPersonalDetailModel.h"

#import <MJExtension/MJExtension.h>
#import "MPHeader.h"

@interface MPOtherHomeVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *myTableView;

@property (nonatomic, strong) MPOtherHomeHeaderView *headerView;

@property (nonatomic, strong) NSMutableArray *dataArr;

@end

@implementation MPOtherHomeVC

#pragma mark - Life cycle 生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"用户昵称";

    if (@available(iOS 11.0, *)) {
        self.myTableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }else{
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    
    [self.view addSubview:self.myTableView];
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.alpha = 0;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.alpha = 1;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
}

#pragma mark - Delegate
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellId = @"cellId";
    MPOtherHomeCommentCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if(!cell) {
        cell = [[MPOtherHomeCommentCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellId];
    }
    
    MPPersonalDetailModel *totalModel = [MPPersonalDetailModel mj_objectWithKeyValues:self.dataArr[indexPath.section]];
    WritingRespDTOListItem *listModel = totalModel.writingRespDTOList[indexPath.row];
    cell.model = listModel;
    cell.dateStr = ((indexPath.row == 0) ? totalModel.dateNote : @"");
    cell.isShortLine = (indexPath.section == 0 && indexPath.row == 0);
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    MPPersonalDetailModel *totalModel = [MPPersonalDetailModel mj_objectWithKeyValues:self.dataArr[indexPath.section]];
    WritingRespDTOListItem *listModel = totalModel.writingRespDTOList[indexPath.row];
    float cellHeight = listModel.cellHeight;
    if(indexPath.row == 0) {
        // 显示日期的cell
        CGSize dateSize = [@"日期模拟仅求高度" boundingRectWithSize:CGSizeMake(kScreenWidth-60, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:20]} context:nil].size;
        cellHeight += dateSize.height;
    }
    return cellHeight;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    MPPersonalDetailModel *totalModel = [MPPersonalDetailModel mj_objectWithKeyValues:self.dataArr[section]];
    return totalModel.writingRespDTOList.count;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if(section == 0) {
        return self.headerView;
    }
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return (section == 0) ? 200 : CGFLOAT_MIN;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if([scrollView isEqual:self.myTableView]) {
        NSLog(@"%f",self.myTableView.contentOffset.y);
        self.navigationController.navigationBar.alpha = scrollView.contentOffset.y/kNavBarHeight;
    }
}

#pragma mark - Getters/Setters
- (UITableView *)myTableView {
    if(!_myTableView) {
        _myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) style:UITableViewStyleGrouped];
        _myTableView.delegate = self;
        _myTableView.dataSource = self;
        _myTableView.showsVerticalScrollIndicator = NO;
        _myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _myTableView;
}

- (MPOtherHomeHeaderView *)headerView {
    if(!_headerView) {
        _headerView = [[MPOtherHomeHeaderView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 200)];
        _headerView.backgroundColor = [UIColor blackColor];
    }
    return _headerView;
}

- (NSMutableArray *)dataArr {
    if(!_dataArr) {
        _dataArr = [NSMutableArray arrayWithObjects:
                    @{
                      @"dateNote" : @"今天",
                      @"writingRespDTOList" : @[@{
                                                    @"id":@391,
                                                    @"title":@"无图",
                                                    @"images":@[],
                                                    @"praiseNum":@2,
                                                    @"commentNum":@4,
                                                    @"praiseState":@5,
                                                    @"createTime":@1340234235,
                                                    @"createTimeStr":@"2022-03-03",
                                                    @"showType":@1,
                                                    @"location":@"浙江·杭州",
                                                    },
                                                @{
                                                    @"id":@391,
                                                    @"title":@"1图1图1图1图1图1图1图1图1图1图1图1图1图1图1图1图1图1图1图1图1图1图1图1图1图1图1图1图1图1图1图1图1图1图1图1图",
                                                    @"images":@[@"12345678"],
                                                    @"praiseNum":@2,
                                                    @"commentNum":@4,
                                                    @"praiseState":@5,
                                                    @"createTime":@1340234235,
                                                    @"createTimeStr":@"2022-03-03",
                                                    @"showType":@1,
                                                    @"location":@"浙江·杭州",
                                                    },
                                                @{
                                                    @"id":@391,
                                                    @"title":@"2图",
                                                    @"images":@[@"12345678",@"12345678"],
                                                    @"praiseNum":@2,
                                                    @"commentNum":@4,
                                                    @"praiseState":@5,
                                                    @"createTime":@1340234235,
                                                    @"createTimeStr":@"2022-03-03",
                                                    @"showType":@1,
                                                    @"location":@"浙江·杭州",
                                                    },
                                                @{
                                                    @"id":@391,
                                                    @"title":@"3图",
                                                    @"images":@[@"12345678",@"12345678",@"12345678"],
                                                    @"praiseNum":@2,
                                                    @"commentNum":@4,
                                                    @"praiseState":@5,
                                                    @"createTime":@1340234235,
                                                    @"createTimeStr":@"2022-03-03",
                                                    @"showType":@1,
                                                    @"location":@"浙江·杭州",
                                                    },
                                                ]
                      },
                    
                    
                    
                    
                    
                    
                    
                    
                    @{
                      @"dateNote" : @"昨天",
                      @"writingRespDTOList" : @[@{
                                                    @"id":@391,
                                                    @"title":@"无图",
                                                    @"images":@[],
                                                    @"praiseNum":@2,
                                                    @"commentNum":@4,
                                                    @"praiseState":@5,
                                                    @"createTime":@1340234235,
                                                    @"createTimeStr":@"2022-03-03",
                                                    @"showType":@1,
                                                    @"location":@"浙江·杭州",
                                                    },
                                                @{
                                                    @"id":@391,
                                                    @"title":@"1图1图1图1图1图1图1图1图1图1图1图1图1图1图1图1图1图1图1图1图1图1图1图1图1图1图1图1图1图1图1图1图1图1图1图1图",
                                                    @"images":@[@"12345678"],
                                                    @"praiseNum":@2,
                                                    @"commentNum":@4,
                                                    @"praiseState":@5,
                                                    @"createTime":@1340234235,
                                                    @"createTimeStr":@"2022-03-03",
                                                    @"showType":@1,
                                                    @"location":@"浙江·杭州",
                                                    },

                                                ]
                      }
                    
                    
                    , nil];
    }
    return _dataArr;
}

@end
